﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace CMTVision.Widgets
{
    //[System.Web.UI.ViewStateModeById]
    public class HtmlDivColumn:System.Web.UI.WebControls.PlaceHolder,CMTVision.Widgets.Common.ITemplateColumn
    {
      
        #region Ctor

        public HtmlDivColumn()
            :base()
        { 
            IsInEditMode = false;
            _columnHoder = new HtmlGenericControlModeById("div");
        }
         

        #endregion
         
        #region Implement ITemplateColumn interface

        public int TemplateDataID
        {
            get;
            set;
        }

        public string ColumnID
        {
            get;
            set;
        }

        public virtual bool IsInEditMode
        {
            get
            {
                return ViewState["IsInEditMode"] == null ? false : Convert.ToBoolean(ViewState["IsInEditMode"]);
            }
            set
            {
                ViewState["IsInEditMode"] = value;
            }
        }

        public System.Web.UI.Control ColumnHolder
        {
            get
            {
                return GetColumnHolder();
            }
        }         

        public virtual void LoadWidgets()
        {
            this.Controls.Clear();
            this.Controls.Add(_columnHoder);
            ColumnHolder.Controls.Clear();
            if (TemplateDataID > 0)
            {
                var templateData = (from td in XDocument.Load(this.Page.Server.MapPath("~/Data/templatedata.xml")).Element("templatedatas").Elements("templatedata")
                                    where td.Element("id").Value == TemplateDataID.ToString()
                                    select td).FirstOrDefault();
                if (templateData != null)
                {
                    var column = (from c in templateData.Element("columns").Elements("column")
                                  where c.Attribute("id").Value == ColumnID.ToString()
                                  select c).FirstOrDefault();
                    if (column != null)
                    {
                        var columnId = column.Attribute("id").Value;
                        var widgets = (from w in column.Element("widgets").Elements("widget")
                                       select w).ToList();
                        foreach (var widget in widgets)
                        {
                            var widgetContainer = CreateNewWidgetContainer();
                            (widgetContainer as System.Web.UI.Control).ID = "Widget" + widget.Attribute("id").Value;
                            widgetContainer.IsInEditMode = IsInEditMode;
                            widgetContainer.LoadWidget(TemplateDataID, ColumnID, widgets.IndexOf(widget)); 
                            ColumnHolder.Controls.Add(widgetContainer as System.Web.UI.Control);
                        }
                    }
                }
            }

            //this.RegisterScript(this.ClientID + "_AddColumnHolderClass",
            //                    String.Format("$('#{0}').addClass('{1}')", ColumnHolder.ClientID, Constants.UI_ColumnHolder_ClassName).ToString());
        }

        public virtual Common.IWidgetContainer CreateNewWidgetContainer()
        {
            var widgetContainerSrc = IsInEditMode ? "~/Modules/BaseWidgetContainer.ascx" : "~/Modules/BaseWidgetContainer.ascx";
            return Page.LoadControl(widgetContainerSrc) as CMTVision.Widgets.Common.IWidgetContainer;
        }

        public virtual void ForceRefresh()
        {
            if (ColumnHolder != null)
            {
                foreach (System.Web.UI.Control control in ColumnHolder.Controls)
                {
                    var widgetContainer = control as CMTVision.Widgets.Common.IWidgetContainer;
                    if (widgetContainer != null)
                    {
                        widgetContainer.IsInEditMode = IsInEditMode;
                        widgetContainer.ForceRefresh();
                    }
                }
            }
        }

        #endregion

        #region Other  Methods

        protected System.Web.UI.Control _columnHoder;
        protected virtual System.Web.UI.Control GetColumnHolder()
        {
            return _columnHoder;
        }
        
        #endregion
          
    } 
}